package com.woniu.controller;

import com.woniu.Result;
import com.woniu.ComplainFeignClient;
import com.woniu.dto.ComplainDto;
import com.woniu.OrderFeignClient;
import com.woniu.pojo.User;
import com.woniu.pojo.dto.UserDto;
import com.woniu.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dong
 * @since 2022-11-16
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private ComplainFeignClient complainFeignClient;

    @ApiOperation("登录操作")
    @PostMapping("/login")
    public Result<String> login(@RequestBody @Validated UserDto userDto){
        return userService.login(userDto);
    }

    @ApiOperation("通过userId查找订单")
    @GetMapping("user-auth/order")
    public Result getByUserId(HttpServletRequest request){
        String userId = request.getHeader("userId");
        return orderFeignClient.getByUserId(Integer.parseInt(userId));
    }

    @ApiOperation("通过orderId提交投诉")
    @PostMapping("user-auth/complain")
    public Result submitComplainByOrderId(@RequestBody ComplainDto complainDto,HttpServletRequest request){
        String userId = request.getHeader("userId");
        complainDto.setUserId((Integer.parseInt(userId)));
        boolean flag = userService.submitComplainByOrderId(complainDto);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.ok(userService.getById(id));
    }


    @ApiOperation("产品查询,有权限操作 测试一")
    @GetMapping("/user-auth/find")
    public Result<String> findAuthProducts01(){

        return Result.ok("user-auth 有权限就可以访问    product微服务");
    }
    @ApiOperation("产品查询,有权限操作 测试二")
    @GetMapping("/shop-auth/find")
    public Result<String> findAuthProducts02(){

        return Result.ok("shop--auth 有权限就可以访问    product微服务");
    }
    @ApiOperation("产品查询,有权限操作 测试三")
    @GetMapping("/admin-auth/find")
    public Result<String> findAuthProducts03(){

        return Result.ok("admin-auth 有权限就可以访问    product微服务");
    }

    @ApiOperation("新增")
    @PostMapping
    public Result add(@RequestBody User user){
        boolean save = userService.save(user);
        return save ? Result.ok() : Result.fail();
    }
}
